#include <Windows.h>
#include "..\SP\Pipeline.h"

LRESULT CALLBACK WindowProc(HWND hWnd,
							UINT msg,
							WPARAM wParam,
							LPARAM lParam)
{
	PAINTSTRUCT ps;

	switch(msg)
	{
	case WM_PAINT:
		BeginPaint(hWnd,&ps);
		EndPaint(hWnd,&ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}

	return DefWindowProc(hWnd, msg, wParam, lParam);
}

int WINAPI WinMain(HINSTANCE hInstance,
				HINSTANCE hPrevInstance,
				LPSTR cmdLine,
				int nCmdShow)
{
	WNDCLASSEX wnd;
	ZeroMemory(&wnd,sizeof(wnd));
	wnd.cbClsExtra = 0;
	wnd.cbSize = sizeof(wnd);
	wnd.cbWndExtra = 0;
	wnd.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	wnd.hCursor = LoadCursor(NULL,IDC_ARROW);
	wnd.hIcon = LoadIcon(NULL,IDI_APPLICATION);
	wnd.hIconSm = LoadIcon(NULL,IDI_APPLICATION);
	wnd.hInstance = hInstance;
	wnd.lpfnWndProc = WindowProc;
	wnd.lpszClassName = "demo";
	wnd.lpszMenuName = NULL;
	wnd.style = CS_VREDRAW | CS_HREDRAW;

	RegisterClassEx(&wnd);

	HWND hWnd = CreateWindowEx(0,"demo","Game",WS_OVERLAPPEDWINDOW,0,0,600,480,NULL,NULL,hInstance,NULL);

	UpdateWindow(hWnd);
	ShowWindow(hWnd,SW_SHOW);

	BITMAPINFO bitmapInfo;
	ZeroMemory(&bitmapInfo,sizeof(bitmapInfo));
	bitmapInfo.bmiHeader.biSize = sizeof(bitmapInfo.bmiHeader);
	bitmapInfo.bmiHeader.biWidth = 600;
	bitmapInfo.bmiHeader.biHeight = 480;
	bitmapInfo.bmiHeader.biPlanes = 1;
	bitmapInfo.bmiHeader.biBitCount = 24;
	bitmapInfo.bmiHeader.biCompression = BI_RGB;

	BYTE* bitmapData;
	HBITMAP hBitmap = CreateDIBSection(GetDC(hWnd), &bitmapInfo, DIB_RGB_COLORS, (VOID**)&bitmapData, NULL, 0);

	SP::Pipeline pipeline;

	MSG msg;

	while (TRUE)
	{
		if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if (msg.message == WM_QUIT)
			{
				break;
			}
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		else
		{
			pipeline.setOutputBuffer(bitmapData);
			pipeline.draw(0);
			StretchDIBits(GetDC(hWnd), 0, 0, 600, 480, 0, 0, 600, 480, bitmapData, &bitmapInfo, DIB_RGB_COLORS,SRCCOPY);
		}
	}

	return msg.wParam;
}